
/**
 * 描述 相关查找排序后的节点
 * @date 2022-03-09
 * @param {array} cells 所有的cells有edge也有node
 * @param {object} startNode 开始查找的节点默认为开始节点
 * @returns {any}
 */
export function getSortAfterNodes(cells) {
  const edges = cells.filter((item) => item?.shape?.includes('edge'));
  const nodes = cells.filter((item)=> item?.shape?.includes('node'));
  // 默认从第一个节点开始找
  let startNode = nodes.find((item) => item?.shape === 'start-node');

  const sortNode = (startNode, arr) => {
    let newArr = arr || [];
    const lineEdge = edges.filter(item=> startNode?.id === item['source'].cell);
    newArr.push(startNode);
    if(lineEdge && lineEdge.length > 0) {
      const nodeItems = nodes.filter(item=> item.id === lineEdge[0]['target'].cell)[0];
      return sortNode(nodeItems, newArr, 'source', 'target');
    }
    return arr;
  };

  let sortArr = [];
  sortArr = sortNode(startNode);
  return [...new Set(sortArr)];
}
